<?php

require_once 'Classes/Article.php';
require_once 'Classes/Model/PDOModel.php';

class ArticleModel extends PDOModel implements Singleton {
    
    
     private static $instance;
     
     public function getInstance(){
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
     }
     
     public function __clone() {
         throw new Exception("You may not try to clone this object");
     }
 
    protected function __construct() {
        parent::__construct();
    }
    
    
    public function addArticle($article) {
        $value = array($article->getName(), $article->getBarcode(), $article->getDescription(), $article->getStatut(), $article->getSubFamilyID(), $article->getStructureID(),$article->getLoanID(),$article->getOptional_1(), $article->getOptional_2(), $article->getOptional_3(), $article->getOptional_4(), $article->getOptional_5(), $article->getOptional_6());
        $sql = 'INSERT INTO Articles (Name, BarCode, Description, Statut, Sub_FamilyID, StructureID, LoanID, Optional_1, Optional_2, Optional_3, Optional_4, Optional_5, Optional_6) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
                   
        $this->PDORequest($sql, $value);
        
    }
    
    public function updateArticle($article) {
        $value = array($article->getName(), $article->getBarcode(), $article->getDescription(), $article->getStatut(), $article->getSubFamilyID(), $article->getStructureID(), $article->getLoanID(), $article->getOptional_1(), $article->getOptional_2(), $article->getOptional_3(), $article->getOptional_4(), $article->getOptional_5(), $article->getOptional_6(), $article->getID());
        
        $this->PDORequest('UPDATE Articles SET Name=?, BarCode=?, Description=?, Statut=?, Sub_FamilyID=?, StructureID=?, LoanID=?, Optional_1=?, Optional_2=?, Optional_3=?, Optional_4=?, Optional_5=?, Optional_6=? WHERE ID=?', $value);
  
    }

    public function deleteArticlesByNameAndSubFamilyId($subFamilyId, $name) {

        $this->PDORequest('DELETE FROM Articles WHERE Name=? AND Sub_FamilyID=?', array($name, $subFamilyId));

    }

    public function deleteArticlesByName($name) {
        
        $this->PDORequest('DELETE FROM Articles WHERE Name=?', array($name));

    }
    
    public function deleteArticlesById($id) {
        
        $this->PDORequest('DELETE FROM Articles WHERE ID=?', array($id));

    }

    public function getAllArticles() {
        $result = $this->PDORequest('SELECT * FROM Articles');
           
        if($result) {
            $arr = array();
            foreach ($result as $article) {
                array_push($arr, new Article($article));
            }
            return $arr;
        }
    }
    
    public function getArticlesById($id) {
        $value = array($id);
        $result = $this->PDORequest('SELECT * FROM Articles WHERE ID=?', $value);        
        if($result) {
            $arr = array();
            foreach ($result as $article) {
                array_push($arr, new Article($article));
            }
            return $arr;
        }

    }
    
     public function getArticlesBySubFamilyId($id) {
        $value = array($id);
        $result = $this->PDORequest('SELECT * FROM Articles WHERE Sub_FamilyID=?', $value);        
        if($result) {
            $arr = array();
            foreach ($result as $article) {
                array_push($arr, new Article($article));
            }
            return $arr;
        }

    }
    
    public function getArticlesByBarCode($barcode) {
        $value = array($barcode);
        $result = $this->PDORequest('SELECT * FROM Articles WHERE BarCode=?', $value);       
        if($result) {
            $arr = array();
            foreach ($result as $article) {
                array_push($arr, new Article($article));
            }
            return $arr;
        }
    }
    
    public function getArticlesByNameAndSubFamilyId($name, $subFamilyId) {
        
        $value = array($name, $subFamilyId);
        $result = $this->PDORequest('SELECT * FROM Articles WHERE Name=? AND Sub_FamilyID=?', $value);
                
        if($result) {
            $arr[count($result)];
            foreach ($result as $article) {
                array_push($arr, new Article ($article));
            }
            return $arr;
        }
    }
	
}

?>
